package com.risk.management.service.impl;

import com.risk.management.entity.ProjectMember;
import com.risk.management.entity.User;
import com.risk.management.mapper.ProjectMemberMapper;
import com.risk.management.mapper.UserMapper;
import com.risk.management.service.ProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目成员服务实现类
 */
@Service
public class ProjectMemberServiceImpl implements ProjectMemberService {

    @Autowired
    private ProjectMemberMapper projectMemberMapper;
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public ProjectMember getProjectMemberById(Long id) {
        return projectMemberMapper.selectById(id);
    }

    @Override
    public List<ProjectMember> getProjectMembersByProjectId(Long projectId) {
        return projectMemberMapper.selectByProjectId(projectId);
    }

    @Override
    public List<ProjectMember> getProjectMembersByUserId(Long userId) {
        return projectMemberMapper.selectByUserId(userId);
    }

    @Override
    public List<User> getUsersByProjectId(Long projectId) {
        List<ProjectMember> members = projectMemberMapper.selectByProjectId(projectId);
        List<Long> userIds = members.stream()
                .map(ProjectMember::getUserId)
                .collect(Collectors.toList());
        
        if (userIds.isEmpty()) {
            return new ArrayList<>();
        }
        
        return userMapper.selectByIds(userIds);
    }

    @Override
    public List<User> getUsersByProjectIdAndRole(Long projectId, Integer role) {
        List<ProjectMember> members = projectMemberMapper.selectByProjectIdAndRole(projectId, role);
        List<Long> userIds = members.stream()
                .map(ProjectMember::getUserId)
                .collect(Collectors.toList());
        
        if (userIds.isEmpty()) {
            return new ArrayList<>();
        }
        
        return userMapper.selectByIds(userIds);
    }

    @Override
    public boolean isMember(Long projectId, Long userId) {
        return projectMemberMapper.checkIsMember(projectId, userId) > 0;
    }

    @Override
    @Transactional
    public boolean addProjectMember(ProjectMember projectMember) {
        // 检查是否已经是项目成员
        if (isMember(projectMember.getProjectId(), projectMember.getUserId())) {
            return false;
        }
        
        // 设置加入时间和创建时间
        Date now = new Date();
        if (projectMember.getJoinTime() == null) {
            projectMember.setJoinTime(now);
        }
        projectMember.setCreateTime(now);
        projectMember.setUpdateTime(now);
        
        // 如果角色为空，设置为普通成员
        if (projectMember.getRole() == null) {
            projectMember.setRole(3); // 3表示普通成员
        }
        
        return projectMemberMapper.insert(projectMember) > 0;
    }

    @Override
    @Transactional
    public boolean addProjectMembers(Long projectId, List<Long> userIds, Integer role) {
        if (projectId == null || userIds == null || userIds.isEmpty()) {
            return false;
        }
        
        // 如果角色为空，设置为普通成员
        if (role == null) {
            role = 3; // 3表示普通成员
        }
        
        Date now = new Date();
        boolean result = true;
        
        for (Long userId : userIds) {
            // 检查是否已经是项目成员
            if (isMember(projectId, userId)) {
                continue;
            }
            
            ProjectMember member = new ProjectMember();
            member.setProjectId(projectId);
            member.setUserId(userId);
            member.setRole(role);
            member.setJoinTime(now);
            member.setCreateTime(now);
            member.setUpdateTime(now);
            
            if (projectMemberMapper.insert(member) <= 0) {
                result = false;
            }
        }
        
        return result;
    }

    @Override
    @Transactional
    public boolean updateProjectMemberRole(Long id, Integer role) {
        ProjectMember member = getProjectMemberById(id);
        if (member == null) {
            return false;
        }
        
        member.setRole(role);
        member.setUpdateTime(new Date());
        
        return projectMemberMapper.update(member) > 0;
    }

    @Override
    @Transactional
    public boolean updateProjectMemberRole(Long projectId, Long userId, Integer role) {
        ProjectMember member = projectMemberMapper.selectByProjectIdAndUserId(projectId, userId);
        if (member == null) {
            return false;
        }
        
        member.setRole(role);
        member.setUpdateTime(new Date());
        
        return projectMemberMapper.update(member) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProjectMember(Long id) {
        return projectMemberMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProjectMember(Long projectId, Long userId) {
        return projectMemberMapper.deleteByProjectIdAndUserId(projectId, userId) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProjectMembers(Long projectId) {
        return projectMemberMapper.deleteByProjectId(projectId) > 0;
    }
} 